package com.bkgy.modular.tbdsjc621digitalevaluation.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.tbdsjc621digitalevaluation.dao.TbDsjC621DigitalEvaluationDao;
import com.bkgy.modular.tbdsjc621digitalevaluation.entity.ExporttbDsjC621DigitalEvaluation;
import com.bkgy.modular.tbdsjc621digitalevaluation.entity.TbDsjC621DigitalEvaluation;
import com.bkgy.modular.tbdsjc621digitalevaluation.service.TbDsjC621DigitalEvaluationService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1720产线数字化评价结果表接口具体实现类
 *
 * @author 生成器生成
 * @date 2022-08-31 16:56:08
 */
@Service
public class TbDsjC621DigitalEvaluationServiceImpl implements TbDsjC621DigitalEvaluationService {

    @Resource
    private TbDsjC621DigitalEvaluationDao tbDsjC621DigitalEvaluationDao;

    /**
     * 分页查看记录
     *
     * @param jsonObject 查询条件类
     */
    public ResultData findDataTbDsjC621DigitalEvaluationByPage(JSONObject jsonObject) {
        try {
            Integer pageIndex = jsonObject.getInteger("pageIndex");
            Integer pageSize = jsonObject.getInteger("pageSize");

            // 条件参数对象
            JSONObject condition = jsonObject.getJSONObject("condition");
            if (null == pageIndex || null == pageSize) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            //分页查询
            List<TbDsjC621DigitalEvaluation> list = tbDsjC621DigitalEvaluationDao.findByPageData((pageIndex - 1) * pageSize,pageSize * pageIndex,condition);
            Integer count = tbDsjC621DigitalEvaluationDao.findByCount(condition);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");



        // 调用service查询出数据
        List<TbDsjC621DigitalEvaluation> data = null;
        List<ExporttbDsjC621DigitalEvaluation> list = new ArrayList<>();
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbDsjC621DigitalEvaluationDao.findByPageData((i - 1) * pageSize, i * pageSize,condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (TbDsjC621DigitalEvaluation item : data) {
                    ExporttbDsjC621DigitalEvaluation exporttbDsjC621DigitalEvaluation = new ExporttbDsjC621DigitalEvaluation();
                    BeanUtils.copyProperties(item,exporttbDsjC621DigitalEvaluation);
                    list.add(exporttbDsjC621DigitalEvaluation);
                }
            }
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\数字化评价.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}